package edu.uestc.canary.service.bean;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.uestc.canary.entity.Category;
import edu.uestc.canary.mapper.CategoryMapper;
import edu.uestc.canary.service.CategoryService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryServiceBean implements CategoryService {
    @Resource private CategoryMapper mapper;

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public List<Category> loadAll() {
        return mapper.findAll();
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public PageInfo<Category> loadAll(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        var result = mapper.findAll();
        return new PageInfo<>(result);
    }

    @Override
    public boolean save(Category category) {
        return mapper.insert(category) == 1;
    }

    @Override
    public boolean removeById(Integer id) {
        return mapper.deleteById(id) == 1;
    }

    @Override
    public boolean modify(Category category) {
        return mapper.updateById(category) == 1;
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public Category findById(Integer id) {
        return mapper.findById(id);
    }
}
